/*
 * Created on 29.07.2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.opencube.data.oracle;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.text.SimpleDateFormat;

import oracle.jdbc.OracleResultSet;
import oracle.jdbc.OracleResultSetMetaData;
import oracle.sql.BLOB;
import oracle.sql.CLOB;

import org.opencube.data.Record;

/**
 * Key-Tec Landsberg
 * 
 * @author at 29.07.2004 12:45:12
 */
public class OracleAxis extends Oracle
{
    /* The special sql type constant = 1111 */
    private static final int ORACLE_SQL_TYPE_SPECIAL = 1111;

    /* The special sql type name constant: CLOB = 'CLOB' */
    private static final String ORACLE_TYPE_NAME_CLOB = "CLOB";

    /* The special sql type name constant: NCLOB = 'NCLOB' */
    private static final String ORACLE_TYPE_NAME_NCLOB = "NCLOB";

    /* The special sql type name constant: NVARCHAR2 = 'NVARCHAR2' */
    private static final String ORACLE_TYPE_NAME_NVARCHAR2 = "NVARCHAR2";

    protected Record mapRecord(ResultSet resultSet, ResultSetMetaData rsMetaData)
            throws SQLException
    {
        OracleResultSet rs = (OracleResultSet) resultSet;
        OracleResultSetMetaData metaData = (OracleResultSetMetaData) rsMetaData;
        int columnCount = metaData.getColumnCount();

        Record result = new Record( columnCount );

        for (int i = 1; i <= columnCount; i++)
        {
            int type = metaData.getColumnType(i);
            String typeName = metaData.getColumnTypeName(i);
            String name = metaData.getColumnName(i);
            Object value = null;
            if (type == ORACLE_SQL_TYPE_SPECIAL                    && !(typeName.equalsIgnoreCase(ORACLE_TYPE_NAME_NVARCHAR2)))
            {
                if (typeName.equalsIgnoreCase(ORACLE_TYPE_NAME_CLOB)
                        || typeName.equalsIgnoreCase(ORACLE_TYPE_NAME_NCLOB))
                {
                    CLOB clob = rs.getCLOB(i);
                    if (clob != null)
                    {
                        value = clob.getSubString(1, (int) clob.length());
                    }
                } else
                {
                    BLOB blob = rs.getBLOB(i);
                    if (blob != null)
                    {
                        value = blob.getBytes(1, (int) blob.length());
                    }
                }
            } else
            {
                if (type == java.sql.Types.DATE)
                {
                    String dt = new String("01.01.2000 01:01:01");
                    if (rs.getDate(i) != null)
                        dt = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss")
                                .format(rs.getTimestamp(i));
                    value = dt;
                } else
                {
                    value = rs.getObject(i);
                }
            }
            result.setValue( i -1, value);
        }
        return result;
    }
}